package com.qlk.vorder.utils;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @User：zhaohengchong
 * @E-mail: zhaohengchong@360buy.com
 * @Date：2011-10-27
 * @Time：下午01:10:01 类说明 导出对象
 */
public class WorkBookObject {

    private HSSFWorkbook wb = new HSSFWorkbook();

    private HSSFSheet sheet;

    private int cellSize;

    private List<String> titleList = new ArrayList<String>();

    public final static int TITLE_ROW = 0;
    /**
     * 行游标
     */
    private int rowCursor = TITLE_ROW;
    public final static String PATTERN_0 = "yyyy-MM-dd";
    public final static String PATTERN_1 = "yyyy-MM-dd HH:mm:ss";

    public WorkBookObject() {

    }

    /**
     * 创建单一默认的sheet
     *
     * @param sheetName
     */
    public WorkBookObject(String sheetName) {
        this.sheet = ExportByPOIUtil.createSheet(wb, sheetName);
    }

    /**
     * 增加标题栏
     *
     * @param rowTitle
     */
    public void addTitle(String rowTitle) {
        titleList.add(rowTitle);
    }

    /**
     * 增加标题栏
     *
     * @param rowTitles
     */
    public void addTitle(String[] rowTitles) {
        if (rowTitles != null && rowTitles.length > 0) {
            for (String rowTitle : rowTitles) {
                titleList.add(rowTitle);
            }
        }
    }

    /**
     * 设置标题栏
     */
    public void saveTitle() {
        if (this.titleList != null && this.titleList.size() != 0) {
            saveTitle(this.titleList);
        }
    }

    /**
     * 设置标题栏
     *
     * @param titleList
     */
    public void saveTitle(List<String> titleList) {
        if (titleList != null && titleList.size() != 0) {
            this.cellSize = titleList.size();
            HSSFRow row = ExportByPOIUtil.createRow(this.sheet, TITLE_ROW);
            row.setHeightInPoints(30);//设置行高为30
            for (int i = 0; i < this.cellSize; i++) {
                HSSFCell cell = ExportByPOIUtil.createCell(row, i);
                cell.setCellValue(titleList.get(i));
            }
        }
        rowCursor++;
    }

    /**
     * 保存数据到 workbook
     *
     * @param <T>
     *            简单的对象
     * @param dataList
     *            对象集合
     * @param T
     * @param tAttrs
     *            需要导出的属性
     */
    public <T> void saveDataList(List<T> dataList, Class<T> T,
                                 String[] tAttrs) {
        try {
            for (T t : dataList) {
                HSSFRow row = ExportByPOIUtil.createRow(this.sheet,
                        this.rowCursor++);
                for (int i = 0; i < cellSize; i++) {
                    HSSFCell cell = ExportByPOIUtil.createCell(row, i);
                    if (tAttrs != null && tAttrs.length != 0) {
                        String methodName = "get"
                                + tAttrs[i].substring(0, 1).toUpperCase()
                                + tAttrs[i].substring(1);
                        Method method = T.getMethod(methodName);
                        Object obj = method.invoke(t);
                        if (obj != null) {
                            if (obj instanceof Integer) {
                                cell.setCellValue((Integer) obj);
                            } else if (obj instanceof Float) {
                                cell.setCellValue((Float) obj);
                            } else if (obj instanceof Double) {
                                cell.setCellValue((Double) obj);
                            } else if (obj instanceof String) {
                                cell.setCellValue((String) obj);
                            } else if (obj instanceof Date) {
                                cell.setCellValue(parseDate2String((Date) obj));
                            } else {
                                cell.setCellValue(obj.toString());
                            }
                        }
                    } else {
                        cell.setCellValue(t.toString());
                    }
                }
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据默认格式转换指定日期
     *
     * @param date
     * @return
     */
    public String parseDate2String(Date date) {
        return parseDate2String(date, PATTERN_1);
    }

    /**
     * 根据指定格式转换指定日期
     *
     * @param date
     * @param pattern
     * @return
     */
    public String parseDate2String(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 设置表格样式
     *
     */
    public void setTitleStyle() {
        this.sheet.setColumnWidth(0,18 * 256);
        this.sheet.setColumnWidth(1,18 * 256);
        this.sheet.setColumnWidth(2,18 * 256);
        this.sheet.setColumnWidth(3,20 * 256);
        this.sheet.setColumnWidth(4,18 * 256);
        this.sheet.setColumnWidth(5,20 * 256);
        this.sheet.setColumnWidth(6,16 * 256);
        this.sheet.setColumnWidth(7,16 * 256);
        this.sheet.setColumnWidth(8,14 * 256);
        this.sheet.setColumnWidth(9,14 * 256);
        this.sheet.setColumnWidth(10,12 * 256);
        this.sheet.setColumnWidth(11,12 * 256);
        this.sheet.setColumnWidth(12,14 * 256);
        this.sheet.setColumnWidth(13,14 * 256);
        this.sheet.setColumnWidth(14,14 * 256);
        this.sheet.setColumnWidth(15,16 * 256);
        this.sheet.setColumnWidth(16,16 * 256);
        this.sheet.setColumnWidth(17,20 * 256);
        this.sheet.setColumnWidth(18,22 * 256);
        this.sheet.setColumnWidth(19,22 * 256);
        this.sheet.setColumnWidth(20,14 * 256);
        this.sheet.setColumnWidth(21,16 * 256);
        this.sheet.setColumnWidth(22,16 * 256);
        this.sheet.setColumnWidth(23,16 * 256);
        this.sheet.setColumnWidth(24,16 * 256);
        this.sheet.setColumnWidth(25,16 * 256);
        this.sheet.setColumnWidth(26,16 * 256);
        this.sheet.setColumnWidth(27,16 * 256);

        if (this.titleList != null && this.titleList.size() != 0) {
            this.cellSize = this.titleList.size();
            HSSFRow row = ExportByPOIUtil.createRow(this.sheet, TITLE_ROW);
            row.setHeightInPoints(30);//设置行高为30
            for (int i = 0; i < this.cellSize; i++) {
                HSSFCell cell = ExportByPOIUtil.createCell(row, i);

                HSSFCellStyle cellStyle = wb.createCellStyle();
                if(i < 17){
                    cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
                }else{
                    cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
                }
                cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

                Font font = wb.createFont();
                font.setFontName("微软雅黑");
                font.setFontHeightInPoints((short) 11);
                cellStyle.setFont(font);

                cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
                cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

                cell.setCellStyle(cellStyle);

                cell.setCellValue(titleList.get(i));
            }
        }
        rowCursor++;
    }

    public HSSFWorkbook getWb() {
        return wb;
    }

    public void setWb(HSSFWorkbook wb) {
        this.wb = wb;
    }

    public HSSFSheet getSheet() {
        return sheet;
    }

    public void setSheet(HSSFSheet sheet) {
        this.sheet = sheet;
    }

    public int getCellSize() {
        return cellSize;
    }

    public void setCellSize(int cellSize) {
        this.cellSize = cellSize;
    }

    public List<String> getTitleList() {
        return titleList;
    }

    public void setTitleList(List<String> titleList) {
        this.titleList = titleList;
    }

    public int getRowCursor() {
        return rowCursor;
    }

    public void setRowCursor(int rowCursor) {
        this.rowCursor = rowCursor;
    }
}
